package com.ai.module.chat.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.Nullable;

/**
 * @author: MrHeg
 * @create: 2023-09-01 22:52
 * @description:
 **/
@Slf4j
@Component
public class SpringUtils<T> implements ApplicationContextAware {
    public static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext application) throws BeansException {
        applicationContext = application;
        log.info("springUtil  init success !!");


    }

    public static <T> T getClazz(Class<T> t, @Nullable String beanName) {
        if (!StringUtils.isBlank(beanName)) {
            return applicationContext.getBean(beanName, t);
        }
        return applicationContext.getBean(t);
    }

    public static <T> T getClazz(Class<T> t) {
        return applicationContext.getBean(t);
    }



}
